<?php

class User_Form_GroupForm extends Pldkp_Form 
{ 
    protected $group = null;
    
    public function __construct($group = null, $options = null)
    {
        $this->group = $group;
        parent::__construct($options);
    }
    
    public function init()
    {
        $this->setName('create_group');

        $name = new Zend_Form_Element_Text('name');
        $name->setRequired(true)
              ->setLabel('Name')
              ->addValidator('NotEmpty');
        
        // Ignore the unique entry for the group itself...
        if ($this->group) {
            $name->addValidator('UniqueEntry', false, array('User_Model_Groups', 'name', array('id' => $this->group->id)));
        } else {
            $name->addValidator('UniqueEntry', false, array('User_Model_Groups', 'name'));
        }
        
        $parentGroup = new Zend_Form_Element_Select('parent_group_id');
        $parentGroup->setLabel('Parent Group')
                    ->addMultiOption(null, '...');
        
        // Build a list of all posible parrent groups
        $groups = User_Model_Groups::getAll();
        foreach ($groups as $group) {
            if ($this->group && $this->group->id == $group->id) {
                continue;
            }
            $parentGroup->addMultiOption($group->id, $group->name);
        }
        
              
        $isDefault = new Zend_Form_Element_Checkbox('is_default');
        $isDefault->setLabel('Is deafult')
                  ->setRequired(true);
              
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($this->group ? 'Edit' : 'Create');
        
        $this->addElements(array($name, $parentGroup, $isDefault, $submit));
    }
}